#include "configmanager.h"
#include <QProcess>
#include <QFile>
#include <QTextStream>
#include <QStandardPaths>
#include <QDir>

ConfigManager::ConfigManager(QObject *parent)
    : QObject(parent)
    , m_settings(new QSettings("OhMyLinux", "CPUSchedulerGUI"))
{
    initDefaultValues();
    loadConfig();
}

ConfigManager::~ConfigManager()
{
    delete m_settings;
}

void ConfigManager::initDefaultValues()
{
    m_checkInterval = 5;
    m_highFreqThreshold = 80;
    m_lowFreqThreshold = 30;
    m_sustainedPeriodHigh = 10;
    m_sustainedPeriodLow = 60;
    m_freqReduction = 25;
    m_freqIncrease = 25;
    m_targetFreqAfterReduction = 75;
    m_ioCriticalThreshold = 90;
    m_ioSevereThreshold = 80;
    m_ioHighThreshold = 70;
    m_logFile = "/var/log/cpu-scheduler.log";
    m_maxLogSizeMB = 5;
    m_maxLogFiles = 5;
}

// Getter methods
int ConfigManager::checkInterval() const
{
    return m_checkInterval;
}

int ConfigManager::highFreqThreshold() const
{
    return m_highFreqThreshold;
}

int ConfigManager::lowFreqThreshold() const
{
    return m_lowFreqThreshold;
}

int ConfigManager::sustainedPeriodHigh() const
{
    return m_sustainedPeriodHigh;
}

int ConfigManager::sustainedPeriodLow() const
{
    return m_sustainedPeriodLow;
}

int ConfigManager::freqReduction() const
{
    return m_freqReduction;
}

int ConfigManager::freqIncrease() const
{
    return m_freqIncrease;
}

int ConfigManager::targetFreqAfterReduction() const
{
    return m_targetFreqAfterReduction;
}

int ConfigManager::ioCriticalThreshold() const
{
    return m_ioCriticalThreshold;
}

int ConfigManager::ioSevereThreshold() const
{
    return m_ioSevereThreshold;
}

int ConfigManager::ioHighThreshold() const
{
    return m_ioHighThreshold;
}

QString ConfigManager::logFile() const
{
    return m_logFile;
}

int ConfigManager::maxLogSizeMB() const
{
    return m_maxLogSizeMB;
}

int ConfigManager::maxLogFiles() const
{
    return m_maxLogFiles;
}

// Setter methods
void ConfigManager::setCheckInterval(int value)
{
    m_checkInterval = value;
}

void ConfigManager::setHighFreqThreshold(int value)
{
    m_highFreqThreshold = value;
}

void ConfigManager::setLowFreqThreshold(int value)
{
    m_lowFreqThreshold = value;
}

void ConfigManager::setSustainedPeriodHigh(int value)
{
    m_sustainedPeriodHigh = value;
}

void ConfigManager::setSustainedPeriodLow(int value)
{
    m_sustainedPeriodLow = value;
}

void ConfigManager::setFreqReduction(int value)
{
    m_freqReduction = value;
}

void ConfigManager::setFreqIncrease(int value)
{
    m_freqIncrease = value;
}

void ConfigManager::setTargetFreqAfterReduction(int value)
{
    m_targetFreqAfterReduction = value;
}

void ConfigManager::setIoCriticalThreshold(int value)
{
    m_ioCriticalThreshold = value;
}

void ConfigManager::setIoSevereThreshold(int value)
{
    m_ioSevereThreshold = value;
}

void ConfigManager::setIoHighThreshold(int value)
{
    m_ioHighThreshold = value;
}

void ConfigManager::setLogFile(const QString &value)
{
    m_logFile = value;
}

void ConfigManager::setMaxLogSizeMB(int value)
{
    m_maxLogSizeMB = value;
}

void ConfigManager::setMaxLogFiles(int value)
{
    m_maxLogFiles = value;
}

bool ConfigManager::isServiceRunning() const
{
    QProcess process;
    process.start("systemctl", QStringList() << "is-active" << "cpu-scheduler.service");
    process.waitForFinished();
    
    QString status = process.readAllStandardOutput().trimmed();
    return (status == "active");
}

bool ConfigManager::startService()
{
    QProcess process;
    process.start("sudo", QStringList() << "systemctl" << "start" << "cpu-scheduler.service");
    process.waitForFinished();
    
    return (process.exitCode() == 0);
}

bool ConfigManager::stopService()
{
    QProcess process;
    process.start("sudo", QStringList() << "systemctl" << "stop" << "cpu-scheduler.service");
    process.waitForFinished();
    
    return (process.exitCode() == 0);
}

bool ConfigManager::restartService()
{
    QProcess process;
    process.start("sudo", QStringList() << "systemctl" << "restart" << "cpu-scheduler.service");
    process.waitForFinished();
    
    return (process.exitCode() == 0);
}

QString ConfigManager::serviceStatus() const
{
    QProcess process;
    process.start("systemctl", QStringList() << "is-active" << "cpu-scheduler.service");
    process.waitForFinished();
    
    return process.readAllStandardOutput().trimmed();
}

void ConfigManager::saveConfig()
{
    m_settings->setValue("checkInterval", m_checkInterval);
    m_settings->setValue("highFreqThreshold", m_highFreqThreshold);
    m_settings->setValue("lowFreqThreshold", m_lowFreqThreshold);
    m_settings->setValue("sustainedPeriodHigh", m_sustainedPeriodHigh);
    m_settings->setValue("sustainedPeriodLow", m_sustainedPeriodLow);
    m_settings->setValue("freqReduction", m_freqReduction);
    m_settings->setValue("freqIncrease", m_freqIncrease);
    m_settings->setValue("targetFreqAfterReduction", m_targetFreqAfterReduction);
    m_settings->setValue("ioCriticalThreshold", m_ioCriticalThreshold);
    m_settings->setValue("ioSevereThreshold", m_ioSevereThreshold);
    m_settings->setValue("ioHighThreshold", m_ioHighThreshold);
    m_settings->setValue("logFile", m_logFile);
    m_settings->setValue("maxLogSizeMB", m_maxLogSizeMB);
    m_settings->setValue("maxLogFiles", m_maxLogFiles);
    
    m_settings->sync();
    
    emit configChanged();
}

void ConfigManager::loadConfig()
{
    m_checkInterval = m_settings->value("checkInterval", m_checkInterval).toInt();
    m_highFreqThreshold = m_settings->value("highFreqThreshold", m_highFreqThreshold).toInt();
    m_lowFreqThreshold = m_settings->value("lowFreqThreshold", m_lowFreqThreshold).toInt();
    m_sustainedPeriodHigh = m_settings->value("sustainedPeriodHigh", m_sustainedPeriodHigh).toInt();
    m_sustainedPeriodLow = m_settings->value("sustainedPeriodLow", m_sustainedPeriodLow).toInt();
    m_freqReduction = m_settings->value("freqReduction", m_freqReduction).toInt();
    m_freqIncrease = m_settings->value("freqIncrease", m_freqIncrease).toInt();
    m_targetFreqAfterReduction = m_settings->value("targetFreqAfterReduction", m_targetFreqAfterReduction).toInt();
    m_ioCriticalThreshold = m_settings->value("ioCriticalThreshold", m_ioCriticalThreshold).toInt();
    m_ioSevereThreshold = m_settings->value("ioSevereThreshold", m_ioSevereThreshold).toInt();
    m_ioHighThreshold = m_settings->value("ioHighThreshold", m_ioHighThreshold).toInt();
    m_logFile = m_settings->value("logFile", m_logFile).toString();
    m_maxLogSizeMB = m_settings->value("maxLogSizeMB", m_maxLogSizeMB).toInt();
    m_maxLogFiles = m_settings->value("maxLogFiles", m_maxLogFiles).toInt();
}